#include "bsp_spi_bus.h"



void spi_bus_init(void)
{
	spi_parameter_struct spi_initpara;                 		// 定义SPI结构体

	rcu_periph_clock_enable(RCU_GPIOG);                   // 使能GPIOG时钟
	rcu_periph_clock_enable(RCU_SPI3);                    // 使能SPI3时钟

	gpio_af_set(GPIOG,GPIO_AF_6,GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13);	//配置spi3 sck、miso、mosi复用端口
	gpio_mode_set(GPIOG,GPIO_MODE_AF,GPIO_PUPD_NONE,GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13);//配置复用模式
	gpio_output_options_set(GPIOG,GPIO_OTYPE_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13);//配置推挽输出50MHZ
	gpio_bit_set(GPIOG,GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13);

	spi_initpara.clock_polarity_phase = SPI_CK_PL_LOW_PH_1EDGE;
	spi_initpara.device_mode = SPI_MASTER;
	spi_initpara.endian = SPI_ENDIAN_MSB;
	spi_initpara.frame_size = SPI_FRAMESIZE_8BIT;
	spi_initpara.nss = SPI_NSS_SOFT;
	spi_initpara.prescale = SPI_PSC_16;
	spi_initpara.trans_mode = SPI_TRANSMODE_FULLDUPLEX;
	spi_init(SPI3,&spi_initpara);

	spi_enable(SPI3);
}

// SPI3读写一个字节
// TxData:要写入的字节
// 返回值:读取到的字节
//-----------------------------------------------------------------------
uint8_t spi_read_write_byte(uint8_t TxData)
{		
	uint8_t TxWait = 0;
	uint8_t RxWait = 0;
	
	// 等待发送缓存为空
	while(spi_i2s_flag_get(SPI3,SPI_FLAG_TBE) == RESET)
	{
		TxWait++;
		if(TxWait>250)	// 等待时间过长则放弃本次读写
		return 0;
	}	

	spi_i2s_data_transmit(SPI3,TxData); // SPI2写一个字节
	
	// 等待接收缓存为空
	while (spi_i2s_flag_get(SPI3,SPI_FLAG_RBNE) == RESET)
	{
		RxWait++;
		if(RxWait>250)	// 等待时间过长则放弃本次读写
		return 0;
	}	  	
	
	return spi_i2s_data_receive(SPI3); // 将读到的字节返回					    
}



/*!
    \brief      send a byte through the SPI interface and return the byte received from the SPI bus
    \param[in]  byte: byte to send
    \param[out] none
    \retval     the value of the received byte
*/
uint8_t spi_TransmitReiceve_byte(uint8_t byte)
{
    // /* loop while data register in not empty */
    // while(RESET == spi_i2s_flag_get(SPI3,SPI_FLAG_TBE));
	
    // /* send byte through the SPI5 peripheral */
    // spi_i2s_data_transmit(SPI3,byte);

    // /* wait to receive a byte */
    // while(RESET == spi_i2s_flag_get(SPI3,SPI_FLAG_RBNE));

    // /* return the byte read from the SPI bus */
    // return(spi_i2s_data_receive(SPI3));
	
	
	while (RESET == spi_i2s_flag_get(SPI3, SPI_FLAG_TBE)) ;
    spi_i2s_data_transmit(SPI3, byte); 
    while (RESET == spi_i2s_flag_get(SPI3, SPI_FLAG_TBE)) ;
	return 0;
}
/*!
    \brief      send a half word through the SPI interface and return the half word received from the SPI bus
    \param[in]  half_word: half word to send
    \param[out] none
    \retval     the value of the received byte
*/
uint16_t spi_TransmitReiceve_halfword(uint16_t half_word)
{
    /* loop while data register in not emplty */
    while(RESET == spi_i2s_flag_get(SPI3,SPI_FLAG_TBE));

    /* send half word through the SPI5 peripheral */
    spi_i2s_data_transmit(SPI3,half_word);

    /* wait to receive a half word */
    while(RESET == spi_i2s_flag_get(SPI3,SPI_FLAG_RBNE));

    /* return the half word read from the SPI bus */
    return spi_i2s_data_receive(SPI3);
}

uint8_t spi_Transmit_bytes(uint8_t* buffer, size_t buff_size)
{

	
	while(buff_size--) {
	while (RESET == spi_i2s_flag_get(SPI3, SPI_FLAG_TBE)) ;
    spi_i2s_data_transmit(SPI3, *buffer); 
    while (RESET == spi_i2s_flag_get(SPI3, SPI_FLAG_TBE)) ;
			
		}
	return 0;
}

void spi_lcd_gpio_Init(void)
{
	/* 使能时钟 */
	rcu_periph_clock_enable(RCU_GPIOC);
	rcu_periph_clock_enable(RCU_GPIOB);

	/* 配置为输出模式  */
	gpio_mode_set(GPIOB,GPIO_MODE_OUTPUT,GPIO_PUPD_PULLUP,GPIO_PIN_13| GPIO_PIN_15);
	gpio_mode_set(GPIOC,GPIO_MODE_OUTPUT,GPIO_PUPD_PULLUP,GPIO_PIN_10| GPIO_PIN_12);

	/* 配置为推挽输出 50MHZ */
	gpio_output_options_set(GPIOB,GPIO_OTYPE_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_13| GPIO_PIN_15);
	gpio_output_options_set(GPIOC,GPIO_OTYPE_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_10| GPIO_PIN_12);

	gpio_bit_write(GPIOB,GPIO_PIN_13|GPIO_PIN_15,SET);

};
void spi_soft_gpio_config(void)
{
	/* 使能时钟 */
	rcu_periph_clock_enable(RCU_GPIOG);

	/* 配置为输出模式 浮空模式 */
	gpio_mode_set(GPIOG,GPIO_MODE_OUTPUT,GPIO_PUPD_NONE,GPIO_PIN_11|GPIO_PIN_13);

	/* 配置为推挽输出 50MHZ */
	gpio_output_options_set(GPIOG,GPIO_OTYPE_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_11|GPIO_PIN_13);

	gpio_bit_write(GPIOG,GPIO_PIN_11|GPIO_PIN_13,RESET);
}

void  SPI_soft_WriteData(u8 Data)
{
	unsigned char i=0;
	for(i=8;i>0;i--)
	{
	  if(Data&0x80)	
	  SPI_MOSI_SET; //输出数据
      else SPI_MOSI_CLR;
	   
      SPI_SCLK_CLR;       
      SPI_SCLK_SET;
      Data<<=1; 
	}
}
uint8_t spi_soft_Transmit_bytes(uint8_t* buffer, size_t buff_size)
{
    // /* loop while data register in not empty */
    // while(RESET == spi_i2s_flag_get(SPI3,SPI_FLAG_TBE));
	
    // /* send byte through the SPI5 peripheral */
    // spi_i2s_data_transmit(SPI3,byte);

    // /* wait to receive a byte */
    // while(RESET == spi_i2s_flag_get(SPI3,SPI_FLAG_RBNE));

    // /* return the byte read from the SPI bus */
    // return(spi_i2s_data_receive(SPI3));
	
		while(buff_size--) {
			spi_read_write_byte(*buffer);
		}
	return 0;
}